/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.timesoft.hibernate.core;

import com.timesoft.common.core.CoreContent;
import com.timesoft.common.hibernate.TransactionalProcessor;
import com.timesoft.hibernate.bean.Customer;
import java.util.Date;
import java.util.HashMap;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author sorasaks
 */
public class CustomerCore extends CoreContent {

    private static final Logger LOG = Logger.getLogger(CustomerCore.class);

    public static Customer getCustomerById(final Long id, final Boolean isActive) {

        final HashMap param = new HashMap();
        new TransactionalProcessor() {

            @Override
            public void process(Session session, Transaction tx) {
                try {
                    Criteria criteria = session.createCriteria(Customer.class);
                    criteria.add(Restrictions.eq("id", id));

                    if (isActive != null) {
                        criteria.add(Restrictions.eq("active", isActive));
                    }

                    Customer customer = (Customer) criteria.uniqueResult();

                    LOG.info("::::::::::::::: customer :::::::::::::::" + customer);

                    param.put(VALUE, customer);
                } catch (HibernateException e) {
                    LOG.error(e.getMessage());
                    param.put(VALUE, null);
                }
            }
        }.process();

        return (Customer) param.get(VALUE);
    }

    public static Customer getCustomerByCustomerCode(final String customerCode, final Boolean isActive) {

        final HashMap param = new HashMap();
        new TransactionalProcessor() {

            @Override
            public void process(Session session, Transaction tx) {
                try {
                    Criteria criteria = session.createCriteria(Customer.class);
                    criteria.add(Restrictions.eq("customerCode", customerCode));
                    
                    if (isActive != null) {
                        criteria.add(Restrictions.eq("active", isActive));
                    }

                    Customer customer = (Customer) criteria.uniqueResult();

                    LOG.info("::::::::::::::: customer :::::::::::::::" + customer);

                    param.put(VALUE, customer);
                } catch (HibernateException e) {
                    LOG.error(e.getMessage());
                    param.put(VALUE, null);
                }
            }
        }.process();

        return (Customer) param.get(VALUE);
    }

    public static Customer getCustomerByNameAndLastName(final String name, final String lastname, final Boolean isActive) {

        final HashMap param = new HashMap();
        new TransactionalProcessor() {

            @Override
            public void process(Session session, Transaction tx) {
                try {
                    Criteria criteria = session.createCriteria(Customer.class);
                    criteria.add(Restrictions.and(Restrictions.eq("name", name),
                            Restrictions.eq("lastname", lastname)));
                    
                    if (isActive != null) {
                        criteria.add(Restrictions.eq("active", isActive));
                    }

                    criteria.setMaxResults(1);

                    Customer customer = (Customer) criteria.uniqueResult();
                    LOG.info("::::::::::::::: customer :::::::::::::::" + customer);

                    param.put(VALUE, customer);
                } catch (HibernateException e) {
                    LOG.error(e.getMessage());
                    param.put(VALUE, null);
                }
            }
        }.process();

        return (Customer) param.get(VALUE);
    }

    public static Customer getCustomerByEmail(final String email, final Boolean isActive) {

        final HashMap param = new HashMap();
        new TransactionalProcessor() {

            @Override
            public void process(Session session, Transaction tx) {
                try {
                    Criteria criteria = session.createCriteria(Customer.class);
                    criteria.add(Restrictions.eq("email", email));
                    
                    if (isActive != null) {
                        criteria.add(Restrictions.eq("active", isActive));
                    }

                    criteria.setMaxResults(1);

                    Customer customer = (Customer) criteria.uniqueResult();
                    LOG.info("::::::::::::::: customer :::::::::::::::" + customer);

                    param.put(VALUE, customer);
                } catch (HibernateException e) {
                    LOG.error(e.getMessage());
                    param.put(VALUE, null);
                }
            }
        }.process();

        return (Customer) param.get(VALUE);
    }
    
    public static Customer getCustomerByEmailAndPassword(final String email, 
            final String password, 
            final Boolean isActive) {

        final HashMap param = new HashMap();
        new TransactionalProcessor() {

            @Override
            public void process(Session session, Transaction tx) {
                try {
                    Criteria criteria = session.createCriteria(Customer.class);
                    criteria.add(Restrictions.eq("email", email));
                    criteria.add(Restrictions.eq("password", password));
                    
                    if (isActive != null) {
                        criteria.add(Restrictions.eq("active", isActive));
                    }

                    criteria.setMaxResults(1);

                    Customer customer = (Customer) criteria.uniqueResult();
                    LOG.info("::::::::::::::: customer :::::::::::::::" + customer);

                    param.put(VALUE, customer);
                } catch (HibernateException e) {
                    LOG.error(e.getMessage());
                    param.put(VALUE, null);
                }
            }
        }.process();

        return (Customer) param.get(VALUE);
    }

    public static Customer getLastCustomerByCustomerCode() {

        final HashMap param = new HashMap();
        new TransactionalProcessor() {

            @Override
            public void process(Session session, Transaction tx) {
                try {
                    Criteria criteria = session.createCriteria(Customer.class);
                    criteria.addOrder(Order.desc("customerCode"));
                    criteria.setMaxResults(1);

                    Customer customer = (Customer) criteria.uniqueResult();
                    LOG.info("::::::::::::::: customer :::::::::::::::" + customer);

                    param.put(VALUE, customer);
                } catch (HibernateException e) {
                    LOG.error(e.getMessage());
                    param.put(VALUE, null);
                }
            }
        }.process();

        return (Customer) param.get(VALUE);
    }

    public static Boolean updateActiveCustomer(final Customer customer, final String userName) {

        final HashMap param = new HashMap();
        new TransactionalProcessor() {

            @Override
            public void process(Session session, Transaction tx) {
                try {
                    generateFootprint(customer, userName);
                    session.update(customer);

                    param.put(VALUE, Boolean.TRUE);
                } catch (HibernateException e) {
                    LOG.error(e.getMessage());
                    param.put(VALUE, Boolean.FALSE);
                }
            }
        }.process();

        return (Boolean) param.get(VALUE);
    }

    public static Boolean save(final Customer customer, final String userName) {

        final HashMap param = new HashMap();
        new TransactionalProcessor() {

            @Override
            public void process(Session session, Transaction tx) {
                try {
                    generateFootprint(customer, userName, userName, new Date());
                    session.save(customer);

                    param.put(VALUE, Boolean.TRUE);
                } catch (HibernateException e) {
                    LOG.error(e.getMessage());
                    param.put(VALUE, Boolean.FALSE);
                }
            }
        }.process();

        return (Boolean) param.get(VALUE);
    }
}
